using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

/// <summary>
/// 使用注意 监听的必须是目标敌人的GA造成的优势GA 不能用于：监听自己回血GA 回血时自己额外回血GA 正确使用 朔击枪 吸血刀
/// </summary>

// 目标模式接口
public interface ITargetMode
{
    List<CombatantView> GetTargets(GameAction listenGA);
}
//英雄自身标模式
public class HeroSelfTM : ITargetMode
{
    public List<CombatantView> GetTargets(GameAction listenGA)
    {
        return new List<CombatantView> { HeroSystem.Instance.heroView };
    }
}
// 全体敌人目标模式
[Serializable]
public class AllEnemiesRETM : ITargetMode
{
    public List<CombatantView> GetTargets(GameAction listenGA)
    {
        return new List<CombatantView>(EnemySystem.Instance.enemyBoardView.EnemyViews);
    }
}

// 同监听GA目标模式（监听的GA必须实现IHasTargets）
[Serializable]
public class SameAsListenerTM : ITargetMode
{
    public List<CombatantView> GetTargets(GameAction listenGA)
    {
        if (listenGA is IHasTargets hasTargets)
            return new List<CombatantView>(hasTargets.Targets);
        return new List<CombatantView>();
    }
}

// 只用IHasTargets接口
public interface IHasTargets
{
    List<CombatantView> Targets { get; }
}

// 监听触发类，增加概率参数
public class ListenTypeRelicEffect<TListenGA, TDoGA> : RelicEffect
    where TListenGA : GameAction, IHasTargets
    where TDoGA : GameAction
{
    private int amount;
    private ITargetMode targetMode;
    private float probability; // 触发概率 0~1f

    public ListenTypeRelicEffect(int amountt, ITargetMode targetMode, float probability = 1f)
    {
        this.amount = amountt;
        this.targetMode = targetMode;
        this.probability = probability;
    }

    public override void OnEnableDo()
    {
        ActionSystem.SubscribeReaction<TListenGA>(Listen, ReactionTiming.POST);
    }

    public override void OnDisableDo()
    {
        ActionSystem.UnsubscribeReaction<TListenGA>(Listen, ReactionTiming.POST);
    }

    private void Listen(TListenGA listenGA)
    {
        if (!IsProbabilitySatisfied())
            return;
        var targets = GetFilteredTargets(listenGA);
        if (!HasValidTargets(targets))
            return;
        CreateAndAddAction(targets);
    }

    /// <summary>
    /// 检查概率是否满足
    /// </summary>
    private bool IsProbabilitySatisfied()
    {
        return Tool.GetRandom(probability);
    }

    /// <summary>
    /// 获取并过滤目标
    /// </summary>
    private List<CombatantView> GetFilteredTargets(TListenGA listenGA)
    {
        var targets = targetMode.GetTargets(listenGA);

        // 根据不同目标模式过滤目标
        if (targetMode is SameAsListenerTM)
        {
            // 过滤掉Hero
            targets.RemoveAll(t => t is HeroView);
        }
        else if (targetMode is HeroSelfTM)
        {
            // 检查第一个GA（监听的GA）的目标是否包含自己
            // 如果包含自己，则返回空列表（不执行）
            if (IsHeroInTargets(listenGA))
            {
                return new List<CombatantView>();
            }
        }

        return targets;
    }

    /// <summary>
    /// 检查英雄是否在目标中（用于HeroSelfTM模式）
    /// </summary>
    private bool IsHeroInTargets(TListenGA listenGA)
    {
        // 由于TListenGA已约束为IHasTargets，这里可以直接使用
        return listenGA.Targets.Contains(HeroSystem.Instance.heroView);
    }

    /// <summary>
    /// 检查是否有有效目标
    /// </summary>
    private bool HasValidTargets(List<CombatantView> targets)
    {
        return targets != null && targets.Count > 0;
    }

    /// <summary>
    /// 创建并添加行动
    /// </summary>
    private void CreateAndAddAction(List<CombatantView> targets)
    {
        var doGA = (TDoGA)Activator.CreateInstance(typeof(TDoGA), amount, targets);
        ActionSystem.Instance.AddReaction(doGA);
    }

}
